﻿var url;
var currentRole;
var currentUser;
var helper = new KssHelper();
var isNewNonHR = true;

jQuery(function () {

    jQuery('#userManagementMenu').addClass('active');
    jQuery('#UserTab').tabs({
        onSelect: function (title, index) {
            if (index == 0) {
                jQuery('#dgUser').datagrid({
                    url: "/service/api.ashx?metdhodName=getAllUser",
                    loadFilter: pagerFilter,
                    onDblClickRow: function (index, fields, value) {
                        editNonHR(fields);
                    }
                });
            }
            else if (index == 1) {
                jQuery('#dgRole').datagrid({
                    url: "/service/api.ashx?metdhodName=getAllRole",
                    loadFilter: pagerFilter,
                    onDblClickRow: function (index, fields, value) {

                        currentUser = fields.UserName;
                        currentRole = fields.RoleName;
                        editRole(fields);
                    }
                });
            }
            return false;
        }
    });
});

function editRole(currentRole) {
    if (currentRole == undefined) {
        currentRole = jQuery(dg).datagrid('getSelected');
    }
    if (currentRole) {
        jQuery('#dlg').dialog('open').dialog('setTitle', 'เปลี่ยนสิทธ์ของ user');
        jQuery('#roleList').combobox('setValue', currentRole.RoleName);
    }
}
function addNonHR() {
    jQuery('#dlgNonHr').dialog('open').dialog('setTitle', 'เพิ่มข้อมูลพนักงานใหม่');
}

function addRole() {
    jQuery('#dlgRole').dialog('open').dialog('setTitle', 'เพิ่มระดับการใช้งาน');
}

function editNonHR(editUser) {
    if (editUser == undefined) {
        editUser = jQuery('#dgNonHR').datagrid('getSelected')
    }
    if (editUser != undefined) {
        jQuery('#dlgNonHr').dialog('open').dialog('setTitle', 'แก้ไขข้อมูลพนักงาน');
        jQuery('#FormNonHr').form('load', editUser);
        isNewNonHR = false;
    }
}

function deleteNonHR(currentRole) {

}


function saveRole() {
    var newRole = jQuery('#roleList').combobox('getValue');
    jQuery.post('/api.ashx?metdhodName=editUserRole', {
        usernameRole: currentUser,
        oldRole: currentRole,
        newRole: newRole
    }, function (result) {
        if (result.success) {
            jQuery('#dg').datagrid('reload'); // reload the user data
            jQuery('#dlg').dialog('close');
        } else {
            jQuery('#dg').datagrid('reload');
            jQuery.messager.show({	// show error message
                title: 'Error',
                msg: result.msg
            });
        }
    }, 'json');

}

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {	// is array
        data = {
            total: data.length,
            rows: data
        };
    }
    var dg = jQuery(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {
                pageNumber: pageNum,
                pageSize: pageSize
            });
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}

function formatDate(val, row) {
    if (val === null || val === '') {
        return null;
    }
    else {
        var d = new Date(parseInt(val.substr(6)));
        return jQuery.datepicker.formatDate('dd-MM-yy', d) + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    }
}

function searchUser() {
    var keyword = jQuery('#searcKeyword').val();
    var searchType = jQuery('#searchType').combobox('getValue');
    jQuery('#dg').datagrid({
        url: "/api.ashx?metdhodName=getalluserrole&keyword=" + escape(keyword) + "&searchType=" + searchType,
        loadFilter: pagerFilter,
        onDblClickRow: function (index, fields, value) {

            currentUser = fields.UserName;
            currentRole = fields.RoleName;
            editRole(fields);
        }
    });
}
function saveNonHR() {
    var d = getAllInput();
    var data2 = JSON.stringify(d);
    if (isNewNonHR) {
        helper.AddHrUser(data2, function (result) {
            jQuery('#dgNonHR').datagrid({ url: "/api.ashx?metdhodName=getNonEmp", loadFilter: pagerFilter });
            if (result.success) {
                jQuery('#dgNonHR').datagrid('reload'); // reload the user data
            } else {
                jQuery('#dgNonHR').datagrid('reload');
                jQuery.messager.show({	// show error message
                    title: 'Error',
                    msg: result.msg
                });
            }
            jQuery('#dlgNonHr').dialog('close');
        });
    }
    else {
        helper.UpdateHrUser(data2, function (result) {
            jQuery('#dgNonHR').datagrid({ url: "/api.ashx?metdhodName=getNonEmp", loadFilter: pagerFilter });
            if (result.success) {
                jQuery('#dgNonHR').datagrid('reload'); // reload the user data
            } else {
                jQuery('#dgNonHR').datagrid('reload');
                jQuery.messager.show({	// show error message
                    title: 'Error',
                    msg: result.msg
                });
            }
            jQuery('#dlgNonHr').dialog('close');
        });
    }
}

function getAllInput() {
    var datas = [];
    var data = {};
    jQuery("form#FormNonHr :input").each(function () {
        var input = jQuery(this); // This is the jquery object of the input, do what you will
        var name = jQuery(input).attr("name");
        if (name != undefined) {
            var value;
            value = input.val();
            data[name] = value;
        }
    });
    datas.push(data);
    return datas;
}